package com.cskaoyan.gateway.controller.shopping;

import com.cskaoyan.gateway.form.shopping.PageInfo;
import com.cskaoyan.gateway.form.shopping.PageResponse;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/shopping")
@Slf4j
@Anoymous
public class ProductController {

    @Reference
    IProductService productService;


    @Anoymous
    @GetMapping("/goods")
    public ResponseData goods(PageInfo pageInfo) {
        log.info("start get goods...." + pageInfo);
        AllProductRequest allProductRequest = new AllProductRequest();
        allProductRequest.setCid(pageInfo.getCid());
        allProductRequest.setPage(pageInfo.getPage());
        allProductRequest.setSize(pageInfo.getSize());
        allProductRequest.setSort(pageInfo.getSort());
        allProductRequest.setPriceGt(pageInfo.getPriceGt());
        allProductRequest.setPriceLte(pageInfo.getPriceLte());

        AllProductResponse allProduct = productService.getAllProduct(allProductRequest);
        if (!ShoppingRetCode.SUCCESS.getCode().equals(allProduct.getCode())) {
            return new ResponseUtil<>().setErrorMsg(allProduct.getMsg());
        }

        // # issue 3 分页查询商品不显示，因为返回的数据类型不对
        PageResponse pageResponse=new PageResponse();
        pageResponse.setData(allProduct.getProductDtoList());
        pageResponse.setTotal(allProduct.getTotal());
        return new ResponseUtil<>().setData(pageResponse);
    }

    @GetMapping("/product/{id}")
    @Anoymous
    public ResponseData product(@PathVariable long id) {

        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(id);

        ProductDetailResponse productDetail
                = productService.getProductDetail(productDetailRequest);

        if (!ShoppingRetCode.SUCCESS.getCode().equals(productDetail.getCode())) {
            return new ResponseUtil<>().setErrorMsg(productDetail.getMsg());
        }
        return new ResponseUtil<>().setData(productDetail.getProductDetailDto());
    }

    @GetMapping("/recommend")
    @Anoymous
    public ResponseData recommendGoods() {

        RecommendResponse recommendGoods = productService.getRecommendGoods();
        if (!ShoppingRetCode.SUCCESS.getCode().equals(recommendGoods.getCode())) {
            return new ResponseUtil<>().setErrorMsg(recommendGoods.getMsg());
        }
        return new ResponseUtil<>().setData(recommendGoods.getPanelContentItemDtos());
    }

}
